package com.example.day10_1;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
TextView text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text=findViewById(R.id.text1);
        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyHelper dbOpenHelper10=new MyHelper(MainActivity.this,"day10",null,2);
                SQLiteDatabase db10=dbOpenHelper10.getWritableDatabase();

                db10.beginTransaction();//开启事务
                int aa=0;
                try {
                    for (int i=0;i<10;i++) {
                        aa++;
                        ContentValues values = new ContentValues();
                        values.put("Name",i+"红孩儿");
                        values.put("Age",154-i+"");
                        values.put("sex","男");
                        db10.insert("user",null,values);
//                        db10.execSQL("insert into User(Name,sex,Age) values('"+i+"风清扬','男',108+i)");
                    }
                    db10.setTransactionSuccessful();//成功

                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    ((TextView)v).setText(aa+"");
                    db10.endTransaction();//结束事务
                    db10.close();//关闭
                }
            }
        });
    }
}
